<template>
  <div>
    <el-row :gutter="20" class="home" style="margin-top: 20px">
      <el-col :span="8">
        <el-card shadow="hover">
          <div class="user">
            <img :src="userImg" />
            <div class="userinfo">
              <p class="name">{{ username }}</p>
              <p class="access">{{ userPermission }}</p>
            </div>
          </div>
          <el-divider></el-divider>
          <div class="login-info">
            <p>
              上次登录时间 : <span>{{ getDate() }}</span>
            </p>
            <p>上次登录地点 : <span>湘潭</span></p>
          </div>
        </el-card>
        <el-card style="margin-top: 20px">
          <el-table :data="tableData" style="width: 100%">
            <el-table-column
              v-for="(item, index) in tableLabel"
              :key="index"
              :prop="index"
              :label="item"
              width="120"
            >
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
      <el-col :span="16">
        <div class="card">
          <el-card
            class="order"
            v-for="el in countData"
            :key="el.name"
            :body-style="{ display: 'flex', padding: 0 }"
          >
            <i
              :class="`el-icon-${el.icon}`"
              :style="{ 'background-color': el.color }"
            ></i>
            <div class="detail">
              <p class="num"><span>¥</span>{{ el.value }}</p>
              <p class="txt">{{ el.name }}</p>
            </div>
          </el-card>
        </div>
        <el-card style="height: 280px">
          <Echart :chartData="echartData.order" style="height: 280px" />
        </el-card>
        <div class="graph">
          <el-card style="height: 265px">
            <Echart :chartData="echartData.user" style="height: 265px" />
          </el-card>
          <el-card style="height: 265px">
            <Echart
              :chartData="echartData.video"
              :isAxisChart="false"
              style="height: 265px"
            />
          </el-card>
          -->
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getData } from "../../api/data";
import Echart from "../../components/Echarts.vue";
export default {
  components: {
    Echart,
  },
  data() {
    return {
      userImg: require("@/assets/images/user.webp"),
      tableData: [],
      countData: [
        {
          name: "今日支付订单",
          value: 1224,
          icon: "success",
          color: "#2ec7c9",
        },
        {
          name: "今日收藏订单",
          value: 210,
          icon: "star-on",
          color: "#ffb980",
        },
        {
          name: "今日未支付订单",
          value: 1234,
          icon: "s-goods",
          color: "#5ab1ef",
        },
        {
          name: "本月支付订单",
          value: 1224,
          icon: "success",
          color: "#2ec7c9",
        },
        {
          name: "本月收藏订单",
          value: 1224,
          icon: "success",
          color: "#2ec7c9",
        },
        {
          name: "本月未支付订单",
          value: 1224,
          icon: "success",
          color: "#2ec7c9",
        },
      ],
      tableLabel: {
        name: "课程",
        todayBuy: "今日购买",
        monthBuy: "月度购买",
        totalBuy: "总购买",
      },
      echartData: {
        order: {},
        user: {},
        video: {},
      },
    };
  },
  computed: {
    username() {
      return this.$store.state.user.username;
    },
    userPermission() {
      return this.$store.state.user.userPermission;
    },
  },
  created() {
    this.$store.commit("user/getUsername");
  },
  mounted() {
    getData().then((res) => {
      const { code, data } = res.data;
      if (code === 200) {
        this.tableData = data.tableData;
        const order = data.orderData;
        const xData = order[0].date;
        const orderKeys = Object.keys(order[0].data[0]);
        const series = [];
        orderKeys.forEach((item) => {
          series.push({
            name: item,
            data: order[0].data.map((el) => el[item]),
            type: "line",
          });
        });
        //
        const options = {
          tooltip: {
            show: true,
          },
          xAxis: {
            data: xData,
          },
          yAxis: {},
          legend: {
            data: orderKeys,
          },
          series,
        };
        this.echartData.order = options;

        //柱状图
        const userOptions = {
          legend: {
            textStyle: {
              color: "#333",
            },
          },
          grid: {
            left: "20%",
          },
          tooltip: {
            trigger: "axis",
          },
          xAxis: {
            type: "category",
            data: data.userData.map((item) => item.date),
            axisLine: {
              lineStyle: {
                color: "#17b3a3",
              },
            },
            axisLabel: {
              interval: 0,
              color: "#333",
            },
          },
          yAxis: [
            {
              type: "value",
              axisLine: {
                lineStyle: {
                  color: "#17b3a3",
                },
              },
            },
          ],
          series: [
            {
              name: "新增用户",
              data: data.userData.map((item) => item.new),
              type: "bar",
            },
            {
              name: "活跃用户",
              data: data.userData.map((item) => item.active),
              type: "bar",
            },
          ],
        };
        this.echartData.user = userOptions;

        //饼图
        const videoOptions = {
          tooltip: {
            trigger: "item",
          },
          series: [
            {
              data: data.videoData,
              type: "pie",
            },
          ],
        };
        this.echartData.video = videoOptions;
      }
    });
  },
  methods: {
    getDate() {
      const date = new Date();
      const year = date.getFullYear();
      let month = date.getMonth() + 1;
      let day = date.getDate();
      if (month < 10) {
        month = "0" + month;
      }
      if (day < 10) {
        day = "0" + day;
      }
      return year + "-" + month + "-" + day;
    },
  },
};
</script>

<style lang="less" scoped>
.home {
  .user {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    img {
      width: 150px;
      height: 150px;
      border-radius: 50%;
    }
    .userinfo {
      margin-left: 20px;
      .name {
        font-size: 24px;
      }
      .access {
        margin-top: -20px;
        color: rgba(167, 157, 153, 0.8);
      }
    }
  }
  .login-info {
    color: rgba(58, 53, 51, 0.8);
    float: left;
    clear: both;
    text-align: left;
    p {
      span {
        margin-left: 50px;
      }
    }
  }
  .card {
    display: flex;
    flex-wrap: wrap;
    .order {
      width: 340px;
      margin: 0 0 20px 20px;
      i {
        width: 60px;
        padding-top: 30px;
        font-size: 30px;
        color: aliceblue;
      }
      .detail {
        text-align: left;
        margin-left: 20px;
        .num {
          font-size: 18px;
          font-weight: bold;
          span {
            margin-right: 5px;
          }
        }
        .txt {
          font-size: 14px;
          color: #606266;
        }
      }
    }
  }
  .graph {
    display: flex;
    margin-top: 20px;
    .el-card {
      width: 530px;
    }
    .el-card:nth-child(2) {
      margin-left: 37px;
    }
  }
}
</style>
